package cn.vcjf.washcar.api.user.controller;


import cn.vcjf.washcar.api.user.entity.Feedback;
import cn.vcjf.washcar.api.user.entity.User;
import cn.vcjf.washcar.api.user.service.FeedbackService;
import cn.vcjf.washcar.api.user.service.UserService;
import cn.vcjf.washcar.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 用户意见反馈表 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-26
 */
@Controller
@RequestMapping("/user/feedback")
public class FeedbackController {
     @Resource
     private FeedbackService  feedbackService;
     @Resource
     private UserService userService;

     @RequestMapping("/addFeedback")
     @ResponseBody
     public TableResult addFeedback(Feedback feedback){
         feedback.preInsert();
         User user = userService.selectById(feedback.getUserId());
         feedback.setCreateUser(user.getNickname());
         boolean b = feedbackService.insert(feedback);
         return TableResult.Result(b);
     }

     @RequestMapping("/updateFeedback")
     @ResponseBody
     public TableResult updateFeedback(Feedback feedback){
         feedback.preUpdate();
        boolean b = feedbackService.updateById(feedback);
         return TableResult.Result(b);
     }

     @RequestMapping("/findFeedbackOne")
     @ResponseBody
     public TableResult findFeedbackOne(Feedback feedback){

        EntityWrapper<Feedback> wrapper = new EntityWrapper<Feedback>();

        if(feedback.getId()!=null){
            wrapper.eq("id",feedback.getId());
        }
        //Todo

        Feedback result = feedbackService.selectOne(wrapper);

		return TableResult.Result(result);
     }

     @RequestMapping("/findFeedbackList")
     @ResponseBody
     public TableResult findFeedbackList(Feedback feedback){

        EntityWrapper<Feedback> wrapper = new EntityWrapper<Feedback>();

		if(feedback.getId()!=null){
            wrapper.eq("id",feedback.getId());
        }
         if(feedback.getUserId()!=null){
             wrapper.eq("userId",feedback.getUserId());
         }
        //Todo
         wrapper.orderBy("createTime",false);

        Page<Feedback> page = feedback.getPageInfo();

        page = feedbackService.selectPage(page,wrapper);
		return TableResult.Result(page);

     }

     @RequestMapping("/deleteFeedback")
     @ResponseBody
     public TableResult deleteFeedback(Feedback feedback){
        feedback.preUpdate();
        boolean b = feedbackService.deleteById(feedback.getId());
        return TableResult.Result(b);
     }

}
